package com.zoe.onelink.circuitbreaker.sentinel;

import com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerFactory;
import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder;
import com.alibaba.csp.sentinel.EntryType;
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.function.Function;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-01-17
 */
public class OnelinkSentinelCircuitBreakerFactory extends SentinelCircuitBreakerFactory {


    private final Function<String, SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> defaultConfiguration = id -> new SentinelConfigBuilder()
            .resourceName(id).entryType(EntryType.OUT).rules(new ArrayList<>()).build();

    @Override
    public CircuitBreaker create(String id) {
        Assert.hasText(id, "A circuitBreaker must have an id.");
        SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations()
                .computeIfAbsent(id, defaultConfiguration);
        return new OnelinkSentinelCircuitBreaker(id, conf.getEntryType(), conf.getRules());
    }

}
